package com.freshfresh.activity.selfcenter;

import java.util.HashMap;
import java.util.Map;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;

public class UpdatePasswordActivity extends BaseActivity implements
		OnClickListener {
	private Button bt_modify_psw;
	private EditText et_new_psw_affirm;
	private EditText et_new_psw_first;
	private EditText et_old_psw;
	private String newPswAffirm;
	private String newPswFirst;
	private String oldPsw;
	Dialog progressDialog;
	
	TextView tv_title;

	private boolean canUpdatePsw() {
		this.oldPsw = this.et_old_psw.getText().toString().trim();
		this.newPswFirst = this.et_new_psw_first.getText().toString().trim();
		this.newPswAffirm = this.et_new_psw_affirm.getText().toString().trim();
		if (TextUtils.isEmpty(this.oldPsw)) {
			Toast.makeText(getApplicationContext(), "旧密码不能为空", 0).show();
			return false;
		}
		if (TextUtils.isEmpty(this.newPswFirst)) {
			Toast.makeText(getApplicationContext(), "新密码不能为空", 0).show();
			return false;
		}
		if (TextUtils.isEmpty(this.newPswAffirm)) {
			Toast.makeText(getApplicationContext(), "确认新密码不能为空", 0).show();
			return false;
		}
		if (!this.newPswFirst.equals(this.newPswAffirm)) {
			Toast.makeText(getApplicationContext(), "两次输入的新密码不一致", 0).show();
			return false;
		}
		return true;
	}

	private void initView() {
		tv_title=(TextView)findViewById(R.id.tv_title);
		tv_title.setText("我的资料");
		findViewById(R.id.rel_back).setOnClickListener(this);
		progressDialog = Utils.initProgressDialog(UpdatePasswordActivity.this);
		this.et_old_psw = ((EditText) findViewById(R.id.et_old_psw));
		this.et_new_psw_first = ((EditText) findViewById(R.id.et_new_psw_first));
		this.et_new_psw_affirm = ((EditText) findViewById(R.id.et_new_psw_affirm));
		this.bt_modify_psw = ((Button) findViewById(R.id.bt_modify_psw));
		this.bt_modify_psw.setOnClickListener(this);
	}

	private void updatePswRequest() {

		progressDialog.show();
		SharedPreferences userShared = FreshConstants
				.getUserShared(UpdatePasswordActivity.this);
		String customerid = userShared.getString("userid", "");
		String token = userShared.getString("token", "");
		String str_mobile = userShared.getString("mobile", "");

		Map<String, String> params = new HashMap<String, String>();
		params.put("customerid", customerid);
		params.put("token", token);

		params.put("oldpassword", this.oldPsw);
		params.put("newpassword", this.newPswFirst);

		executeRequest(new StringRequest(UrlConstants.getUserUpdatePwd, params,
				new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("arg0个人信息是多少………………", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Toast.makeText(UpdatePasswordActivity.this, "修改成功",
									1).show();
							Intent intent=new Intent(UpdatePasswordActivity.this,LoginActivity.class);
							startActivity(intent);
							UpdatePasswordActivity.this.finish();

						} else if (Utils.parseJsonStr(arg0).get("result")
								.toString().equals("0")) {
							if (Utils.parseJsonStr(arg0).get("errorcode")
									.toString()
									.equals("old password is wrong.")) {
								showToast(UpdatePasswordActivity.this,
										"原始密码输入有误,请重新输入");
								UpdatePasswordActivity.this.et_old_psw
										.setText("");
								return;
							}
							showToast(UpdatePasswordActivity.this, "密码修改失败");
							return;
						}
						{

						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						progressDialog.dismiss();
					}
				}));

	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.ac_update_password);
		initView();
	}

	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.rel_back:
			finish();
			break;
		default:
		case R.id.bt_modify_psw:
			if(canUpdatePsw()){
			updatePswRequest();
			}
		}
	}
}